19. Instantiate an Object

Instantiate an Object

Now it's time to use your matrix class in a program! The C++ syntax for instantiating an object is like this:

Classname objectname(inputs for initializing an object of Classname);

Then you can access any public variables like:

objectname.variablename

And you can access your public functions with:

objectname.methodname(inputs)

Remember that any private variables or functions will not be available to your program. That was why you wrote the public get and set functions for your private variables.

Gaussian.cpp Example

Before you start using your matrix class, here is a reminder of what the main.cpp looked like for the Gaussian.cpp example:

#include <iostream>
#include "gaussian.h"

int main ()
{

    Gaussian mygaussian(30.0,20.0);
    Gaussian othergaussian(10.0,30.0);

    std::cout << "average " << mygaussian.getMu() << std::endl;
    std::cout << "evaluation " << mygaussian.evaluate(15.0) << std::endl;

    std::cout << "mul results sigma " << 
               mygaussian.mul(othergaussian).getSigma2() << std::endl;
    std::cout << "mul results average " << 
               mygaussian.mul(othergaussian).getMu() << std::endl;

    std::cout << "add results sigma " << 
               mygaussian.add(othergaussian).getSigma2() << std::endl;
    std::cout << "add results average " << 
               mygaussian.add(othergaussian).getMu() << std::endl;

    return 0;
}

Now it's your turn to instantiate a Matrix object. You will find some starter code below with a few TODOs.

Start Quiz:

#include <iostream>
#include <vector>
#include "matrix.h"

int main () {
    
    // assign a 7x5 matrix to the variable initial_grid
    // all values in the matrix are 0.4
	std::vector <std:: vector <float> > 
	    initial_grid (7, std::vector <float>(5, 0.4));

    // TODO: Use the initial grid variable to instantiate a matrix object
    // The matrix object should be called matrixa
    
    // TODO: Use the matrix_print() method to print out matrixa
    
    // TODO: Print out the number of rows in matrixa. You will need
    // to use the getRows() function and std::cout
    
    // TODO: Print out the number of columns in matrixa 
    
    // TODO: Take the transpose of matrixa and store the results in
    // a variable called transposea

    // TODO: Print out transposea

    // Now you will use another 7x5 matrix called matrixb to 
    // give the results of the matrix_addition function
    
    // 7x5 2-dimensional vector with values 0.2
	std::vector <std:: vector <float> > 
	    second_grid (7, std::vector <float>(5, 0.2));
    
    // TODO: Instantiate an object called matrixb. Use the second_grid
    // variable as the input to initialize matrixb
    
    // TOOD: Add matrixa and matrixb. Store the results in a new matrix
    // variable called matrixsum
    
    // TODO: Print out the matrix contained in the matrixsum variable

    return 0;
}
// Nothing to do here

#include "matrix.h"

Matrix::Matrix() {
    std::vector <std:: vector <float> > initial_grid (10, std::vector <float>(5, 0.5));
    grid = initial_grid;
    rows = initial_grid.size();
    cols = initial_grid[0].size();

}

Matrix::Matrix(std::vector <std:: vector <float> > initial_grid) {
    grid = initial_grid;
    rows = initial_grid.size();
    cols = initial_grid[0].size();
}

void Matrix::setGrid(std::vector< std::vector<float> > new_grid) {
    grid = new_grid;
    rows = new_grid.size();
    cols = new_grid[0].size();

}

std::vector< std::vector<float> > Matrix::getGrid() {
    return grid;
}

std::vector<float>::size_type Matrix::getRows() {
    return rows;
}

std::vector<float>::size_type Matrix::getCols() {
    return cols;
}

Matrix Matrix::matrix_transpose() {
    std::vector< std::vector<float> > new_grid;
    std::vector<float> row;

    for (int i = 0; i < cols; i++) {
        row.clear();

        for (int j = 0; j < rows; j++) {
            row.push_back(grid[j][i]); 
        }
        new_grid.push_back(row);
    }

    return Matrix(new_grid);
}

Matrix Matrix::matrix_addition(Matrix other) {

    if ((rows != other.getRows()) || (cols != other.getCols())) {
        throw std::invalid_argument( "matrices are not the same size" );
    }

    std::vector< std::vector<float> > othergrid = other.getGrid();

    std::vector< std::vector<float> > result;

    std::vector<float> new_row;

    for (int i = 0; i < rows; i++) {
        new_row.clear();
        for (int j = 0; j < cols; j++) {
            new_row.push_back(grid[i][j] + othergrid[i][j]);
        }
        result.push_back(new_row);
    }

    return Matrix(result);
}

void Matrix::matrix_print() {

    std::cout << std::endl;

    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            std::cout << grid[i][j] << " ";
        }
        std::cout << std::endl;
    }
    std::cout << std::endl;
}
// Nothing to do here
#ifndef MATRIX_H
#define MATRIX_H

#include <vector>
#include <iostream>
#include <stdexcept>

#include <vector>

class Matrix
{
    private:

        std::vector< std::vector<float> > grid;
        std::vector<float>::size_type rows;
        std::vector<float>::size_type cols;

    public:

        // constructor functions
        Matrix ();
        Matrix (std::vector< std::vector<float> >);

        // set functions
        void setGrid(std::vector< std::vector<float> >);

        // get functions
        std::vector< std::vector<float> > getGrid();
        std::vector<float>::size_type getRows();
        std::vector<float>::size_type getCols();

        // matrix functions
        Matrix matrix_transpose();
        Matrix matrix_addition(Matrix);

        // matrix printing
        void matrix_print();

};

#endif /* MATRIX_H */

Solution main.cpp

#include <iostream>
#include <vector>
#include "matrix.h"

int main () {

    // assign a 7x5 matrix to the variable initial_grid
    // all values in the matrix are 0.4
    std::vector <std:: vector <float> > 
        initial_grid (7, std::vector <float>(5, 0.4));

    // TODO: Use the initial grid variable to instantiate a matrix object
    // The matrix object should be called matrixa
    Matrix matrixa(initial_grid);

    // TODO: Use the matrix_print() method to print out matrixa
    matrixa.matrix_print();

    // TODO: Print out the number of rows in matrixa. You will need
    // to use the getRows() function and std::cout
    std::cout << matrixa.getRows();

    // TODO: Print out the number of columns in matrixa 
    std::cout << matrixa.getCols();

    // TODO: Take the transpose of matrixa and store the results in
    // a variable called transposea
    Matrix transposea = matrixa.matrix_transpose();

    // TODO: Print out transposea
    transposea.matrix_print();

    // Now you will use another 7x5 matrix called matrixb to 
    // give the results of the matrix_addition function

    // 7x5 2-dimensional vector with values 0.2
    std::vector <std:: vector <float> > 
        second_grid (7, std::vector <float>(5, 0.2));

    // TODO: Instantiate an object called matrixb. Use the second_grid
    // variable as the input to initialize matrixb
    Matrix matrixb(second_grid);

    // TOOD: Add matrixa and matrixb. Store the results in a new matrix
    // variable called matrixsum
    Matrix matrixsum(matrixa.matrix_addition(matrixb));

    // TODO: Print out the matrix contained in the matrixsum variable
    matrixsum.matrix_print();

    return 0;
}